const { think } = require('thinkjs');
const Base = require('./base.js');
// const BigNumber = require('bignumber.js');
const crypto = require('crypto');

module.exports = class extends Base {
  async indexAction() {
    const postValues = this.post();
    const ts = this.get('ts');
    const sign = this.get('sign');

    const str = JSON.stringify(postValues) + ';' + ts;

    const hmac = crypto.createHmac(
      'sha256',
      'gp9XSWC2APKENFwqBxBmoGwA0pyyve7E'
    );

    hmac.update(str);

    const res = new Buffer.from(hmac.digest('hex')).toString('base64');
    if (sign === res) {
      let status = 0;
      if (postValues.event === 'AIInterviewStartEvent') {
        status = 1;
      } else if (postValues.event === 'AIInterviewEndEvent') {
        status = 2;
      } else if (postValues.event === 'AIReportCreated') {
        status = 3;
      }

      if (status === 2 || status === 3) {
        const info = await this.model('process')
          .where({ orderToken: postValues.orderToken })
          .find();

        if (info.status === 1) {
          // this.model('user_list')
          //   .where({ id: info.user_id })
          //   .increment('interview', 1);
          // this.model('process')
          //   .where({ orderToken: postValues.orderToken })
          //   .update({
          //     status: status,
          //     ts: postValues.ts,
          //   });
        }

        // if (info.status =2== 2 && status === 3) {
        //   this.model('process')
        //     .where({ orderToken: postValues.orderToken })
        //     .update({
        //       status: status,
        //       ts: postValues.ts,
        //     });
        // }

        if (status === 3) {
          const reprotInterview = await this.service('process', 'admin').report(
            postValues.orderToken
          );
          if (info.interview_status === 'INTERVIEW_FINISH') {
            this.model('process')
              .where({
                orderToken: postValues.orderToken,
              })
              .update({
                status: status,
                ts: postValues.ts,
                score: reprotInterview.data.list[0].score,
                summary_txt: reprotInterview.data.list[0].summaryText.sprintf,
                summary_tag:
                  reprotInterview.data.list[0].summaryText.variables.join(','),
              });
          } else {
            this.model('process')
              .where({
                orderToken: postValues.orderToken,
              })
              .update({
                ts: postValues.ts,
                score: reprotInterview.data.list[0].score,
                summary_txt: reprotInterview.data.list[0].summaryText.sprintf,
                summary_tag:
                  reprotInterview.data.list[0].summaryText.variables.join(','),
              });
          }

          this.model('user_list')
            .where({ id: info.user_id })
            .update({
              tag: reprotInterview.data.list[0].tag.length
                ? reprotInterview.data.list[0].tag.join(',')
                : '',
            });
        }
      } else {
        // const info = await this.model('process')
        //   .where({ orderToken: postValues.orderToken })
        //   .find();
        // this.model('process')
        //   .where({ orderToken: postValues.orderToken })
        //   .update({
        //     status: status,
        //     ts: postValues.ts,
        //   });
      }
      this.model('process')
        .where({ orderToken: postValues.orderToken })
        .update({
          status: status,
          ts: postValues.ts,
        });
    }
  }
};
